#!/usr/bin/env python3
"""
调试编辑课程功能的脚本
"""

import sys
import os
import traceback

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from system.lms_system import LMSSystem
from managers.course_manager import CourseManager
from models.course import Course

def test_edit_course():
    """测试编辑课程功能"""
    print("=== 开始测试编辑课程功能 ===")
    
    try:
        # 初始化系统
        lms = LMSSystem()
        lms.initialize()
        
        # 获取课程管理器
        course_manager = lms.course_manager
        
        # 获取一门课程进行测试
        course = course_manager.get_course('course_001')
        if not course:
            print("错误: 未找到course_001课程")
            return False
            
        print(f"找到课程: {course.name}")
        print(f"课程原始数据:")
        print(f"  - name: {getattr(course, 'name', 'N/A')}")
        print(f"  - description: {getattr(course, 'description', 'N/A')}")
        print(f"  - instructor_name: {getattr(course, 'instructor_name', 'N/A')}")

        print(f"  - credits: {getattr(course, 'credits', 'N/A')}")
        
        # 测试更新课程
        update_data = {
            'name': '更新后的课程名称',
            'description': '更新后的课程描述',
            'instructor_name': '新教师姓名',

            'credits': 4
        }
        
        print(f"\n尝试更新课程数据: {update_data}")
        
        # 调用update_course方法
        success = course_manager.update_course('course_001', update_data)
        
        if success:
            print("✓ 课程更新成功!")
            
            # 验证更新结果
            updated_course = course_manager.get_course('course_001')
            print(f"\n更新后的课程数据:")
            print(f"  - name: {getattr(updated_course, 'name', 'N/A')}")
            print(f"  - description: {getattr(updated_course, 'description', 'N/A')}")
            print(f"  - instructor_name: {getattr(updated_course, 'instructor_name', 'N/A')}")

            print(f"  - credits: {getattr(updated_course, 'credits', 'N/A')}")
            
            return True
        else:
            print("✗ 课程更新失败!")
            return False
            
    except Exception as e:
        print(f"✗ 测试过程中发生异常: {type(e).__name__}: {e}")
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = test_edit_course()
    print(f"\n=== 测试结果: {'通过' if success else '失败'} ===")